/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - core/container
// Copyright (c) 2014.  All Rights Reserved
//
// File:		AEArrayUtil.cpp
// Author:		Gianluca Belardelli
// Date:		27/05/2014
//
/////////////////////////////////////////////////////////////////////////
#include <AECore.h>

AEUINT32 AE_CALL AEArrayUtil::Reserve( AEMemoryAllocator &maAlloc, void *lpArray, AEINT32 nReqElem, AEINT32 nSizeElem )
{
	AETBaseArray<int> *lpPtr = (AETBaseArray<int> *)lpArray;
	AEINT32 nSize = nSizeElem * nReqElem;

	if( lpPtr->m_lpData == 0L )
	{
		lpPtr->m_lpData = (int *)maAlloc.BufAlloc( nSize );
	}
	else
	{
		const AEINT32 nOldSize = maAlloc.GetAllocatedSize( lpArray, 0 );
		if( nSize > nOldSize )
			lpPtr->m_lpData = (int *)maAlloc.BufRealloc( lpArray, nOldSize, nSize );
	}

	lpPtr->m_nCapacityAndFlags = nSizeElem / nSize;

	return 0;
}

void AE_CALL AEArrayUtil::ReserveMore( AEMemoryAllocator &maAlloc, void *lpArray, AEINT32 nSizeElem )
{
	AETBaseArray<int> *lpPtr = (AETBaseArray<int> *)lpArray;
	const AEINT32 nSize = nSizeElem;

	if( lpPtr->m_lpData == 0L )
	{
		lpPtr->m_lpData = (int *)maAlloc.BufAlloc( nSizeElem );
	}
	else
	{
		const AEINT32 nOldSize = maAlloc.GetAllocatedSize( lpPtr->m_lpData, 0 );
		lpPtr->m_lpData = (int *)maAlloc.BufRealloc( lpPtr->m_lpData, nOldSize, nSizeElem );
	}

	lpPtr->m_nCapacityAndFlags = nSizeElem / nSize;
}

void AE_CALL AEArrayUtil::Reduce( AEMemoryAllocator &maAlloc, void *lpArray, AEINT32 nSizeElem, char *lpInplaceMem, AEINT32 nRequestedCapacity )
{
	// TODO
}